from app import app
from flask import render_template, flash, redirect, url_for, request, make_response, jsonify
from app import db, my_session
import json
import re
import uuid
from datetime import datetime as dt
from pydantic import BaseModel


@app.route('/')
def hello():
    return render_template('index.html',
                           title='Вывод операций ЮКАССЫ!')


@app.route('/add_order/<string:sum_pay>', methods=['GET', 'POST'])
def creater_order(sum_pay):
    title = 'Работа с платежами!'
    print('codeReg editIdea ===== ', sum_pay)
    print()
    return render_template('index.html',
                           title=title)


@app.route('/amount/', methods=['POST'])
def get_order():
    input_json = request.get_json(force=True)
    print('input_json ===== ', input_json)
    order = {"uuid_order": str(uuid.uuid4()),
             "size_pay": input_json['amount'],
             "date_order": dt.now(),
             "status_order": "succeeded"}

    db.InsertOrder(order)
    print()
    return order

@app.route('/info_pay/', methods=['POST'])
def info_pay():
    input_json = request.get_json(force=True)
    print('input_json ===== ', input_json)
    answ = db.get_info_order(input_json['pay_id'])
    print(answ[0])
    # class Answer(BaseModel):
    #     status: str
    #     income_amount: dict
    #     cancellation_details: dict

    ans_json = {'status': answ[0][3],
                "income_amount": {"value": answ[0][1]},
                "cancellation_details": {"reason": "other"}}
    return ans_json
